package com.huawei.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.ddm.v1.DdmClient;
import com.huaweicloud.sdk.ddm.v1.model.ShowInstanceParamRequest;
import com.huaweicloud.sdk.ddm.v1.model.ShowInstanceParamResponse;
import com.huaweicloud.sdk.ddm.v1.model.UpdateInstanceParamRequest;
import com.huaweicloud.sdk.ddm.v1.model.UpdateInstanceParamResponse;
import com.huaweicloud.sdk.ddm.v1.model.UpdateParametersReq;
import com.huaweicloud.sdk.ddm.v1.model.UpdateParametersReqValues;
import com.huaweicloud.sdk.ddm.v1.region.DdmRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.function.Consumer;

public class ModifyParamDemo {

    private static final Logger LOGGER = LoggerFactory.getLogger(ModifyParamDemo.class.getName());

    /**
     * args[0] = <<YOUR AK>>
     * args[1] = <<YOUR SK>>
     * args[2] = <<YOUR INSTANCE_ID>>
     * args[3] = <<YOUR REGION_ID>>
     **/
    public static void main(String[] args) {
        if (args.length != 4) {
            LOGGER.error("The expected number of parameters is 4");
            return;
        }

        String ak = args[0];
        String sk = args[1];
        String instanceId = args[2];
        String regionId = args[3];

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);
        DdmClient client = DdmClient.newBuilder()
                .withCredential(auth)
                .withRegion(DdmRegion.valueOf(regionId))
                .build();

        // 查询DDM实例的参数详情
        showInstanceParam(client, instanceId);

        // 修改DDM实例参数
        // 修改 慢查询耗时阈值为1s，SQL执行超时阈值为30s
        UpdateParametersReqValues values = new UpdateParametersReqValues()
                .withLongQueryTime("1")
                .withSqlExecuteTimeout("120");

        modifyInstanceParam(client, instanceId, values);

        // 查询DDM实例的参数详情
        showInstanceParam(client, instanceId);
    }

    private static void showInstanceParam(DdmClient client, String instanceId) {
        ShowInstanceParamRequest request = new ShowInstanceParamRequest().withInstanceId(instanceId);

        Consumer<ShowInstanceParamRequest> consumer = (req) -> {
            ShowInstanceParamResponse response = client.showInstanceParam(req);
            LOGGER.info(response.toString());
        };

        executeRequest(consumer, request);
    }

    private static void modifyInstanceParam(DdmClient client, String instanceId, UpdateParametersReqValues values) {
        UpdateInstanceParamRequest request = new UpdateInstanceParamRequest()
                .withInstanceId(instanceId)
                .withBody(new UpdateParametersReq().withValues(values));

        Consumer<UpdateInstanceParamRequest> consumer = (req) -> {
            UpdateInstanceParamResponse response = client.updateInstanceParam(req);
            LOGGER.info(response.toString());
        };

        executeRequest(consumer, request);
    }

    private static <T> void executeRequest(Consumer<T> consumer, T request) {
        try {
            consumer.accept(request);
        } catch (ConnectionException e) {
            LOGGER.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            LOGGER.error("RequestTimeoutException ", e);
        } catch (ServiceResponseException e) {
            LOGGER.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}",
                    e.getHttpStatusCode(), e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            LOGGER.error("Unexpected error occurred: ", e);
        }
    }

}
